package org.jsola.hr.web;

import lombok.extern.slf4j.Slf4j;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.CompanyContractQuotaRecordAddDTO;
import org.jsola.hr.dto.CompanyContractQuotaRecordUpdateDTO;
import org.jsola.hr.query.CompanyContractQuotaRecordQuery;
import org.jsola.hr.service.ICompanyContractQuotaRecordService;
import org.jsola.hr.vo.CompanyContractQuotaRecordVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 用工单位合同配额明细表
 *
 * @author zhr
 */
@Slf4j
@RestController("hrCompanyContractQuotaRecordController")
@RequestMapping(HrConstants.API_PATH + "/v1/companyContractQuotaRecords")
public class CompanyContractQuotaRecordController implements ICompanyContractQuotaRecordControllerAPI {

    @Autowired
    private ICompanyContractQuotaRecordService companyContractQuotaRecordService;

    @Override
    @PostMapping
    public Result<CompanyContractQuotaRecordVO> save(@Valid @RequestBody CompanyContractQuotaRecordAddDTO companyContractQuotaRecordAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增公司合同配额权限校验

        return Result.success(
                companyContractQuotaRecordService.save(companyContractQuotaRecordAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody CompanyContractQuotaRecordUpdateDTO companyContractQuotaRecordUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改公司合同配额权限校验

        int count = companyContractQuotaRecordService.update(companyContractQuotaRecordUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping(value = "/{companyContractQuotaRecordId}")
    public Result<Integer> delete(@PathVariable String companyContractQuotaRecordId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除公司合同配额权限校验

        int count = companyContractQuotaRecordService.deleteByIds(tokenUser, companyContractQuotaRecordId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] companyContractQuotaRecordIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除公司合同配额权限校验

        int count = companyContractQuotaRecordService.deleteByIds(tokenUser, companyContractQuotaRecordIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{companyContractQuotaRecordId}")
    public Result<CompanyContractQuotaRecordVO> findById(@PathVariable String companyContractQuotaRecordId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看公司合同配额权限校验

        return Result.success(
                companyContractQuotaRecordService.selectById(companyContractQuotaRecordId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<CompanyContractQuotaRecordVO>> listAll(@Valid CompanyContractQuotaRecordQuery companyContractQuotaRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看公司合同配额权限校验

        return Result.success(
                companyContractQuotaRecordService.select(companyContractQuotaRecordQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<CompanyContractQuotaRecordVO>> page(@Valid CompanyContractQuotaRecordQuery companyContractQuotaRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                companyContractQuotaRecordService.selectPage(companyContractQuotaRecordQuery, tokenUser.getSiteId())
        );
    }

    /**
     * 获取租户合同配额
     */
    @GetMapping(value = "/getBySite")
    public Result<CompanyContractQuotaRecordVO> getBySite() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看公司合同配额权限校验

        return Result.success(
                companyContractQuotaRecordService.getBySite(tokenUser.getSiteId())
        );
    }


}

